<template>
  <el-table-column class-name="order-table-col">
    <template #default>
      <el-table
        :data="tableData"
        border
        style="width: 100%"
        :show-header="true"
        :span-method="spanMethod"
        row-class-name="product-row"
      >
        <!-- 商品信息行（跨所有列） -->
        <el-table-column label="商品信息" width="100%">
          <template #default="{ row }">
            <div class="product-header">
              <span>商品ID：{{ row.id }}</span>
              <span>内部编码：{{ row.internalCode }}</span>
              <span
                >推广链接：
                <a :href="row.promotionLink" target="_blank" icon="el-icon-link"
                  >{{ row.promotionLink }}
                </a>
                <Icon v-copyText="row.promotionLink" icon="material-symbols:content-copy" />
              </span>
              <span>创建时间：{{ row.createTime }}</span>
              <span>修改时间：{{ row.updateTime }}</span>
              <div class="actions">
                <el-button size="small">编辑</el-button>
                <el-button size="small">复制</el-button>
                <el-button size="small">预览</el-button>
                <el-button size="small">更多</el-button>
              </div>
            </div>
          </template>
        </el-table-column>

        <!-- 实际数据列 -->
        <el-table-column
          v-for="(col, index) in columns"
          :key="index"
          :prop="col.prop"
          :label="col.label"
          :width="col.width"
        >
          <template #header>
            <!-- <div class="product-header">
            <span>{{ col.label }}</span>
          </div> -->
          </template>
          <template #default="{ row }">
            <template v-if="col.prop === 'price'">
              {{ formatPrice(row.price) }}
            </template>
            <template v-else-if="col.prop === 'template'">
              {{ row.template }} <el-checkbox v-model="row.templateChecked" />
            </template>
            <template v-else>
              {{ row[col.prop] }}
            </template>
          </template>
        </el-table-column>
      </el-table>
    </template>
  </el-table-column>
</template>

<script setup>
import { ref } from 'vue'
defineOptions({
  name: 'ProductTableColumn'
})
const columns = ref([
  { prop: 'scan', label: '商品封面', width: '120' },
  { prop: 'title', label: '商品标题/商品标题（外部）', width: '200' },
  { prop: 'region', label: '地区/语言', width: '120' },
  { prop: 'website', label: '所属网站', width: '150' },
  { prop: 'category', label: '商品分类', width: '120' },
  { prop: 'type', label: '商品形态', width: '100' },
  { prop: 'price', label: '价格', width: '120' },
  { prop: 'department', label: '部门/创建者', width: '150' },
  { prop: 'keywords', label: '关键词', width: '180' },
  { prop: 'template', label: '模板', width: '120' }
])

const tableData = ref([
  {
    id: 'sd1237409197330',
    internalCode: 'sd123740919733',
    promotionLink: 'https://huaban.com/follow',
    createTime: '2025-12-12 12:12:12',
    updateTime: '2025-12-12 12:12:12',
    scan: '新网数查询并下载的种子资源用户...',
    title: 'XXXXXXXXXXX',
    region: '台湾/繁体中文',
    website: 'XXXXXXXX',
    category: '--',
    type: '套餐',
    price: 10000.0,
    discountPrice: 1000.59,
    department: '售后',
    keywords: '新网数查询并下载的种子资源用户...',
    template: '模板05',
    templateChecked: true
  }
  // 更多数据...
])

const formatPrice = (price) => {
  return `¥${price.toFixed(2)} (¥${(price * 0.1).toFixed(2)})`
}

// 合并单元格方法（用于商品信息行）
const spanMethod = ({ rowIndex, columnIndex }) => {
  if (columnIndex === 0) {
    return {
      rowspan: 1,
      colspan: columns.value.length + 1 // 跨所有列
    }
  }
}
</script>

<style lang="scss" scoped>
.product-table {
  width: 100%;
}

.product-header {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
  padding: 10px;
  background: #f5f7fa;
  border-bottom: 1px solid #ebeef5;
}

.product-header span {
  white-space: nowrap;
}

.actions {
  margin-left: auto;
}

:deep(.product-row) {
  td {
    vertical-align: top;
    padding: 0 !important;
  }

  .cell {
    padding: 10px;
  }
}

:deep(.el-table__body tr:hover td) {
  background-color: #f5f7fa !important;
}
</style>
